using System;
using System.Collections.Generic;
using System.Text;


using IGlobalist.Cms.Model;
using IGlobalist.Cms.DataAccess;

namespace IGlobalist.Cms.Bll
{
    public class RoleController: BaseController
    {
        private static IRole ConvertCmsObject2Role(ICmsObject obj)
        {
            IRole role = new Role();
            role.FromCmsObject(obj);
            return role;
        }
        private static IList<IRole> ConvertListObject2ListRoles(IList<ICmsObject> list)
        {
            IList<IRole> roles = new List<IRole>();
            if (list!=null)
            foreach (ICmsObject obj in list)
            {
                roles.Add(ConvertCmsObject2Role(obj));
            }

            return roles;
        }
       

        public static IRole GetRole(long id)
        {           
            return ConvertCmsObject2Role( _Ds.GetObject(id));
        }

        public static IRole GetRole(string name)
        {
            ICmsClass clazz = ClassController.GetClassByUsageType((short)CmsObjectType.Role, UsageType.Both);

            try
            {
                IList<IRole> list = GetRoleList(clazz.ID);
            
                foreach (IRole role in list)
                {
                    if (role.Name.Equals(name, StringComparison.OrdinalIgnoreCase))
                    {
                        return role;
                    }
                }
            }
            catch
            {
             
            }
            return null;
        }

        public static IList<IRole> GetRoleList(long classID)
        {
            return ConvertListObject2ListRoles(_Ds.GetObjectListByClassID(classID));            
        }

        public static long InsertRole(IRole role)
        {                  
            //role.ExtPropertyList
            return _Ds.InsertObject((ICmsObject) role);
        }


        public static void UpdateRole(IRole role)
        {
            _Ds.UpdateObject((ICmsObject) role);
        }

        public static void DeleteRole(long id)
        {
            _Ds.DeleteObject(id);
        }

     
    }
}
